package com.gerryrun.lib_skin;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;

public class SkinResource {

    private Context context;
    private String mPackageName = "";
    private boolean isDefaultSkin = true;

    //本包 资源
    private Resources mAppResources;
    //皮肤包资源
    private Resources mSkinResource;

    private volatile static SkinResource instance;

    public static void init(Context context) {
        if (instance == null) {
            synchronized (SkinResource.class) {
                if (instance == null) {
                    instance = new SkinResource(context);
                }
            }
        }
    }

    public static SkinResource getInstance() {
        return instance;
    }

    public SkinResource(Context context) {
        this.context = context;
        this.mAppResources = context.getResources();
    }

    /**
     * 设置 皮肤资源
     *
     * @param skinResources   皮肤包资源
     * @param skinPackageName 包名
     */
    public void applySkin(Resources skinResources, String skinPackageName) {
        mPackageName = skinPackageName;
        mSkinResource = skinResources;
        isDefaultSkin = !(skinResources != null && skinPackageName != null);
    }

    /**
     * 1.通过原始ID，获取自己的资源名字
     * 2.根据名字和类型，获取皮肤包中的资源ID
     */
    public int getIdentifier(int resId) {

        if (isDefaultSkin) {
            return resId;
        }
        //资源名称
        String resName = mAppResources.getResourceEntryName(resId);
        //资源类型
        String resType = mAppResources.getResourceTypeName(resId);

        if (mSkinResource == null) return resId;
        return mSkinResource.getIdentifier(resName, resType, mPackageName);
    }

    /**
     * 根据主APP内的资源ID，获取皮肤包内的 color值
     *
     * @param resId 资源ID
     * @return 皮肤包内的资源ID
     */
    public int getColor(int resId) {

        if (isDefaultSkin) {
            return mAppResources.getColor(resId);
        }
        int skinId = getIdentifier(resId);
        if (skinId == 0) {
            return mAppResources.getColor(resId);
        }
        return mSkinResource.getColor(skinId);
    }

    /**
     * 获取皮肤包中的drawable资源
     *
     * @param resId 资源ID
     * @return 皮肤包中的资源
     */
    private Drawable getDrawable(int resId) {
        if (isDefaultSkin) {
            return mAppResources.getDrawable(resId);
        }
        int skinId = getIdentifier(resId);
        if (skinId == 0 || mSkinResource == null) {
            return mAppResources.getDrawable(resId);
        }
        Drawable skinBack = mSkinResource.getDrawable(resId);
        return skinBack;
    }

    /**
     * 获取背景资源 可能是color 或者是drawable
     *
     * @param resId 资源ID
     * @return color 或者是drawable资源
     */
    public Object getBackground(int resId) {

        String resType = mAppResources.getResourceTypeName(resId);
        if ("color".equals(resType)) {
            return getColor(resId);
        } else {
            return getDrawable(resId);
        }
    }

    //重置状态
    public ColorStateList getColorStateList(int resId) {
        if (isDefaultSkin) {
            return mAppResources.getColorStateList(resId);
        }
        int skinId = getIdentifier(resId);
        if (skinId == 0 || mSkinResource == null) {
            return mAppResources.getColorStateList(resId);
        }
        return mSkinResource.getColorStateList(skinId);
    }

    public void setAppResources(Resources appResources) {
        this.mAppResources = appResources;
    }

    public void reset() {
        mPackageName = "";
        isDefaultSkin = true;
        mSkinResource = null;
    }
}
